package com.leil.nio.channel;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelInputDemo {

    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        FileChannel channel = null;

        try {
            fileInputStream = new FileInputStream("d:/file-channel.txt");
            channel = fileInputStream.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(1024);

            int read = channel.read(buffer);

            String content = new String(buffer.array(), 0, read);
            System.out.println("读出的内容为：" + content);
            fileInputStream.close();
            channel.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

}
